<template>
  <el-card class="rankCard" shadow="hover" @mousewheel="handleWheel">
    <el-card class="rankTabCard" shadow="hover">
      <!-- rank 左侧TAB切换 -->
      <el-tabs
        class="rankLeftTab"
        v-model="activeName"
        @tab-click="handleClickTab"
      >
        <el-tab-pane class="rankTabPane" label="Top榜" name="first">
          <div
            class="text item"
            :class="rId === top.id ? 'active' : ''"
            v-for="top in listTop"
            :key="top.id"
            @click="handleIsChecked(top.id)"
          >
            <img :src="top.coverImgUrl" alt="" />
            <div class="rankLeftTabText">
              <p class="itemTitle">{{ top.name }}</p>
              <p class="itemText">{{ top.updateFrequency }}</p>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane class="rankTabPane" label="特色榜" name="second">
          <div
            class="text item"
            v-for="feature in listFeature"
            :class="rId === feature.id ? 'active' : ''"
            :key="feature.id"
            @click="handleIsChecked(feature.id)"
          >
            <img :src="feature.coverImgUrl" alt="" />
            <div class="rankLeftTabText">
              <p class="itemTitle">{{ feature.name }}</p>
              <p class="itemText">{{ feature.updateFrequency }}</p>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane class="rankTabPane" label="场景榜" name="third">
          <div
            class="text item"
            :class="rId === other.id ? 'active' : ''"
            v-for="other in listOther"
            :key="other.id"
            @click="handleIsChecked(other.id)"
          >
            <img :src="other.coverImgUrl" alt="" />
            <div class="rankLeftTabText">
              <p class="itemTitle">{{ other.name }}</p>
              <p class="itemText">{{ other.updateFrequency }}</p>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- rank右侧展示区 -->
    <main class="rankShow">
      <!-- rank右侧 顶部展示 -->
      <nav class="rankShowTitle">
        <div class="rankShowImg">
          <img :src="playlist.coverImgUrl" alt="" />
        </div>
        <div class="rankShowTitleContent">
          <div class="contentTitle">
            {{ playlist.name }}
            <span
              >({{
                $time.formartData(playlist.updateTime, "MM月dd日")
              }}
              更新)</span
            >
          </div>
          <div class="contentInformation">
            <span>
              <i class="iconfont icon-playnum"></i>
              <i>{{ $time.formartNum(playlist.playCount) }}次</i>
            </span>
            <span>
              <i class="iconfont icon-collect"></i>
              <i>{{ $time.formartNum(playlist.subscribedCount) }}</i>
            </span>
            <span>
              <i class="iconfont icon-comment"></i>
              <i>{{ $time.formartNum(playlist.commentCount) }}</i>
            </span>
          </div>
          <div class="rankShowDesc">
            {{ playlist.description }}
          </div>
        </div>
      </nav>
      <!-- rank歌曲列表 播放区域  -->
      <div class="songHeader">
        <h4>
          歌曲列表
          <em>{{songSum + '首歌'}}</em>
        </h4>
        <span @click="playAllSong">
          <i class="iconfont icon-audio-play"></i>
          播放全部
        </span>
      </div>
      <SongList v-if="song.length" :song="song" :stripe="true"/>
    </main>
  </el-card>
</template>

<script>
import { mapActions, mapState } from "vuex";
import SongList from "../../components/common/SongList";
import { reqGetTopPlayList } from "../../api/allTopList";
import { formartSongInfo } from "../../utils/songInfo";
export default {
  name: "Rank",
  data() {
    return {
      activeName: "first",
      rId: 0,
      list: [], 
      type: "Top",
      playlist: {},
      privileges: [],
      // 歌曲列表
      song: [],
      songSum: 0
    };
  },
  methods: {
    handleWheel(){
      console.log('handleWheel')
    },
    // 将歌单全部歌曲添加到播放列表中
    playAllSong(){
      this.playAll({ list: this.song})
    },
    /* ...mapMutations(["ALLTOPLIST"]), */
    ...mapActions(["getAllTopList", 'playAll']),
    // 用于触发type切换函数
    handleClickTab(tab) {
      // console.log(tab, event)
      if (tab.name === "first") {
        this.handleType("Top");
        return;
      }
      if (tab.name === "second") {
        this.handleType("Feature");
        return;
      }
      if (tab.name === "third") {
        this.handleType("Other");
        return;
      }
      console.log("clickTab")
    },
    // 处理选中
    handleIsChecked(id) {
      this.rId = id;
    },
    // 处理对应的榜单
    handleType(type) {
      this.type = type;
      this.list = this["list" + this.type];
      // console.log('list', this.list)
      // 取对应list的第一项为默认值
      this.rId = this["list" + type][0].id;
      // console.log(this.rId)
    },
    // 歌曲的信息处理
    handleSong_(list, license) {
      const songInfoList = [];
      list.map((item, index) => {
        if (item.id) {
          // 是否有版权播放
          item.license = !license[index].cp;
          songInfoList.push(formartSongInfo(item, index));
        }
      });
      // console.log(songInfoList);
      return songInfoList
    },
    // 获取对应榜单信息的函数
    async getTopPlayList(){
      // 获取对应榜单的信息
      const { playlist, privileges } = await reqGetTopPlayList(this.rId);
      // console.log(playList)
      (this.playlist = playlist), (this.privileges = privileges);
      this.song = this.handleSong_(playlist.tracks, privileges);
      this.songSum = this.song.length
    }
  },
  computed: {
    ...mapState({
      listTop: (state) => state.topList.listTop,
      listFeature: (state) => state.topList.listFeature,
      listOther: (state) => state.topList.listOther,
    }),
  },
  components: {
    SongList,
  },
  async mounted() {
    // 获取所有榜单
    await this.getAllTopList();
    /* .then((...rest) => {
      this.listTop.isCheck = false;
      this.listFeature.isCheck = false;
      this.listOther.isCheck = false;
      console.log(rest);
    }); */
    this.handleType("Top");
    this.getTopPlayList()
  },
  watch: {
    rId(newVal) {
      this.rId = newVal;
      /* if (this.rId) {} */
      this.getTopPlayList()
    },
  },
};
</script>

<style lang="less" scoped>
.rankCard {
  top: 38px;
  left: 0;
  right: 0;
  position: absolute;
  min-width: 1200px;
  padding: 30px;
  display: flex;
  .rankTabCard {
    margin: 0;
    width: 300px;
    padding: 0px 0px 10px 0px;
    .rankLeftTab {
      .active {
        font-weight: bold;
      }
      .rankTabPane {
        width: 275px;
        .active {
          background: rgba(224, 207, 175, 40%);
        }
        .text {
          display: flex;
          align-items: center;
          padding: 10px 0px;
          img {
            width: 60px;
            height: 60px;
          }
          .rankLeftTabText {
            padding-left: 20px;
            p {
              margin: 0;
            }
            .itemTitle {
              line-height: 30px;
              font-weight: bold;
              padding-bottom: 10px;
            }
            .itemText {
              font-size: 12px;
              line-height: 12px;
              color: #999;
            }
          }
        }
      }
    }
  }
  .rankShow {
    padding-left: 40px;
    flex: 1;
    width: 880px;
    .rankShowTitle {
      display: flex;
      .rankShowImg {
        width: 150px;
        height: 150px;
        padding: 5px;
        border: 1px solid #eee;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .rankShowTitleContent {
        flex: 1;
        padding-left: 40px;
        font-size: 14px;
        color: #999;
        .contentTitle {
          font-size: 24px;
          font-weight: bold;
          color: #333;
          padding-bottom: 20px;
          span {
            padding-left: 20px;
            font-size: 14px;
            color: #999;
            font-weight: normal;
          }
        }
        .contentInformation {
          span {
            padding: 0px 20px 5px 0px;
            line-height: 16px;
            i {
              font-style: normal;
            }
          }
        }
        .rankShowDesc {
          margin-top: 5px;
        }
      }
    }
    .songHeader {
      display: flex;
      padding: 30px 0 10px;
      h4 {
        margin: 0;
        flex: 1;
        font-size: 20px;
        line-height: 40px;
        em {
          font-style: normal;
          font-size: 12px;
          font-weight: normal;
          color: #666;
          padding-left: 10px;
        }
      }
      span {
        color: #fff;
        background: #ff641e;
        display: flex;
        line-height: 16px;
        align-items: center;
        border-radius: 50px;
        padding: 7px 20px;
        cursor: pointer;
        margin: 5px 0px 5px 15px;
      }
    }
  }
}
</style>
